package com.hudilao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hudilao.dto.DishSaleDTO;
import com.hudilao.entity.OrderDetail;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderDetailMapper extends BaseMapper<OrderDetail> {

    /**
     * 根据订单id查询订单详情
     * @param orderId 订单id
     * @return  订单对应的多条订单详情
     */
    @Select("select * from order_detail where order_id=#{orderId}")
    List<OrderDetail> getOrderDetailsByOrderId(long orderId);

    @Select("select * from order_detail where customer_id=#{customerID}")
    List<OrderDetail> getOrderDetailsByCustomerId(long customerId);


    // 查询订单详情中的菜品销售数据
    @Select("SELECT item_id AS dishId, item_name AS dishName, SUM(quantity) AS quantity " +
            "FROM order_detail " +
            "WHERE create_time >= #{startTime} AND type = 1 " + // type = 1 代表单独菜品
            "GROUP BY item_id, item_name " +
            "ORDER BY quantity DESC")
    List<DishSaleDTO> selectDishSales(@Param("startTime") LocalDateTime startTime);


    // 查询套餐详情中的菜品销售数据
    @Select("SELECT d.dish_id AS dishId, d.name AS dishName, SUM(d.copies * od.quantity) AS quantity " +
            "FROM order_detail od " +
            "JOIN setmeal_dish d ON od.item_id = d.setmeal_id " +
            "WHERE od.create_time >= #{startTime} AND od.type = 2 " + // type = 2 代表套餐
            "GROUP BY d.dish_id, d.name " +
            "ORDER BY quantity DESC")
    List<DishSaleDTO> selectSetmealDishSales(@Param("startTime") LocalDateTime startTime);
}
